<template>
  <el-button size="small" type="primary" @click="handleAdd()">
    新增
  </el-button>
  <el-card>

    <!--查询条件-->
    <el-form :inline="true" :model="condition" class="demo-form-inline">
      <el-form-item label="标题：">
        <el-input v-model="condition.title" placeholder="请输入标题" clearable />
      </el-form-item>
      <el-form-item label="路径ID：">
        <el-input v-model="condition.path_id" placeholder="请输入路径ID" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchTask()">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="task_id" label="任务ID" width="80" />
      <el-table-column prop="path_id" label="路径ID" width="80" />
      <el-table-column prop="title" label="标题" />
      <el-table-column prop="description" label="描述" />
      <el-table-column prop="day_number" label="天数" width="80" />
      <el-table-column prop="is_required" label="是否必填" width="80">
        <template #default="scope">
          {{ scope.row.is_required === 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="handleView(scope.row)">
            查看
          </el-button>
          <el-button size="small" type="primary" @click="handleEdit(scope.row)">
            编辑
          </el-button>
          <el-popconfirm title="您确定要删除吗?" @confirm="handleRemove(scope.row.task_id)">
            <template #reference>
              <el-button size="small" type="warning">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="demo-pagination-block">
      <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[6, 7, 8, 9]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
    <!-- 查看对话框 -->
    <el-dialog v-model="viewDialogVisible" title="任务详情" width="50%">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="任务ID">{{ currentTask.task_id }}</el-descriptions-item>
        <el-descriptions-item label="路径ID">{{ currentTask.path_id }}</el-descriptions-item>
        <el-descriptions-item label="标题">{{ currentTask.title }}</el-descriptions-item>
        <el-descriptions-item label="描述">{{ currentTask.description }}</el-descriptions-item>
        <el-descriptions-item label="天数">{{ currentTask.day_number }}</el-descriptions-item>
        <el-descriptions-item label="是否必填">{{ currentTask.is_required === 1 ? '是' : '否' }}</el-descriptions-item>
        <el-descriptions-item label="内容">{{ currentTask.content }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 编辑对话框 -->
    <el-dialog v-model="editDialogVisible" :title="editDialogTitle" width="50%">
      <el-form :model="currentTask" label-width="80px">
        <el-form-item label="任务ID">
          <el-input v-model="currentTask.task_id" disabled />
        </el-form-item>
        <el-form-item label="路径ID">
          <el-input v-model="currentTask.path_id" />
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="currentTask.title" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="currentTask.description" />
        </el-form-item>
        <el-form-item label="天数">
          <el-input v-model="currentTask.day_number" />
        </el-form-item>
        <el-form-item label="是否必填">
          <el-select v-model="currentTask.is_required" placeholder="请选择">
            <el-option label="是" value="1" />
            <el-option label="否" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="currentTask.content" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axiosObject from '@/main.js';

const tableData = ref([])
const viewDialogVisible = ref(false)
const editDialogTitle = ref("")
const editDialogVisible = ref(false)
const currentTask = ref({})

//查询条件
const condition = ref({ "title": "", "path_id": "" })

//分页元素
const currentPage = ref(1)
const pageSize = ref(8)
const total = ref(0)

//页面加载完成后自动查询数据
onMounted(() => {
  findTaskList();
})

//分页函数：handleCurrentChange
const handleCurrentChange = () => {
  findTaskList();
}

//分页的函数：handleSizeChange，当pageSize发生改变时调用此函数
const handleSizeChange = () => {
  findTaskList();
}

//带条件查询
const searchTask = () => {
  findTaskList();
}

const handleView = (row) => {
  currentTask.value = { ...row }
  viewDialogVisible.value = true
}

const handleAdd = () => {
  editDialogTitle.value = "添加任务";
  currentTask.value = {};
  editDialogVisible.value = true;
}

//编辑界面
const handleEdit = (row) => {
  editDialogTitle.value = "编辑任务";
  currentTask.value = { ...row }
  editDialogVisible.value = true
}

//根据id删除
const handleRemove = async (id) => {
  await axiosObject.delete("task/" + id);
  await findTaskList();
}

const handleSave = async () => {
  //如果currentTask.value.task_id有值
  if (currentTask.value.task_id) {
    //修改，ajax引擎对象向后端发起put请求
    await axiosObject.put("task", currentTask.value);
  } else {
    //新增，ajax引擎对象向后端发起post请求
    await axiosObject.post("task", currentTask.value);
  }

  await findTaskList();
  editDialogVisible.value = false
}

const findTaskList = async () => {
  const payload = {
    ...condition.value,
    page: currentPage.value,
    size: pageSize.value
  };
try {

  const result = await axiosObject.post('/task/list', payload);
  console.log(result.data);

  tableData.value = result.data.resultData.list || [];
  total.value = result.data.resultData.total || 0;
} catch (error) {
  console.error("获取工具列表失败:", error);
  tableData.value = [];
  total.value = 0;
}
};
</script>

<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
